#include "volumedisplay.h"
#include "ui_volumedisplay.h"


VolumeDisplay::VolumeDisplay(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VolumeDisplay)
{
    ui->setupUi(this);
    hide();
    muted=false;
    volumePercent=50;
    ui->volumeLevel->setValue(volumePercent);
    hideTimer=new QTimer(this);
    setStyleSheet("background-color: rgb(0,0,0)");
    setWindowOpacity(0.8);
    //connect(hideTimer, SIGNAL(timeout()), this, SLOT(setVisible(false)));
    connect(hideTimer, SIGNAL(timeout()), this, SLOT(hide()));
}

VolumeDisplay::~VolumeDisplay()
{
    delete ui;
}

int VolumeDisplay::getVolume() {
    return volumePercent;
}

void VolumeDisplay::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void VolumeDisplay::increaseVolume() {
    volumePercent+=1;
    muted=false;
    ui->volumeImage->setIcon(QIcon(":/imgs/3F00Button_Unmute.png"));
    if(volumePercent>100) {
        volumePercent = 100;
    }
    ui->volumeLevel->setValue(volumePercent);
    ui->retranslateUi(this);
    show();
    releaseKeyboard();
    hideTimer->stop();
    hideTimer->start(2000);
    ui->label->setText(QString::number(volumePercent));
    ui->label->repaint();
    emit volumeChanged(volumePercent);
}

void VolumeDisplay::decreaseVolume() {
    volumePercent-=1;
    muted=false;
    ui->volumeImage->setIcon(QIcon(":/imgs/3F00Button_Unmute.png"));
    if(volumePercent<0) {
        volumePercent = 0;
    }
    ui->volumeLevel->setValue(volumePercent);
    ui->retranslateUi(this);
    show();
    releaseKeyboard();
    hideTimer->stop();
    hideTimer->start(2000);
    ui->label->clear();
    ui->label->setText(QString::number(volumePercent));
    emit volumeChanged(volumePercent);
}

void VolumeDisplay::mute() {
    if(muted) {
        ui->label->clear();
        ui->label->setText(QString::number(volumePercent));
        ui->volumeLevel->setValue(volumePercent);
        ui->volumeImage->setIcon(QIcon(":/imgs/3F00Button_Unmute.png"));
    } else{
        ui->label->clear();
        ui->label->setText(QString::number(0));
        ui->volumeLevel->setValue(0);
        ui->volumeImage->setIcon(QIcon(":/imgs/3F00Button_Mute.png"));
    }
    muted=!muted;
    show();
    releaseKeyboard();
    hideTimer->stop();
    hideTimer->start(2000);
}

void VolumeDisplay::setVolume(int volume) {
    volumePercent=volume;
}
